package cn.edu.scau.cmi.yangzhengkai.comprehension.client;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import org.hibernate.Session;
import org.hibernate.Transaction;
import cn.edu.scau.cmi.yangzhengkai.comprehension.dao.StudentDAO;
import cn.edu.scau.cmi.yangzhengkai.comprehension.dao.TeacherDAO;
import cn.edu.scau.cmi.yangzhengkai.comprehension.domain.Student;
import cn.edu.scau.cmi.yangzhengkai.comprehension.domain.Teacher;
import cn.edu.scau.cmi.yangzhengkai.comprehension.util.ScauCmiHibernateSessionFactoryUtil;

// 使用hiberate操作数据库的客户端
public class ComprehensionHibernateSpringClient {
	public static void main(String[] args) {
		
		Menu1 menu1 = new Menu1();
		menu1.showMenu();
	}
	
	// 添加学生
	static void newStudent(Student student) {
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();		// 创建Session对象	
		Transaction transaction = session.beginTransaction();		// 开启事务
		
		session.save(student);		
		transaction.commit();		// 提交事务
		session.close();
	}
	
	// 删除学生(根据主键id)
	static void deleteStudent(Student student) {
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
				
		session.delete(student);
		transaction.commit();
		session.close();
	}
	
	// 修改学生
	static void updateStudent(Student student) {
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
		
		Student temp = (Student)session.get(Student.class, student.getId());
		temp.setName(student.getName());
		transaction.commit();
		session.close();
	}
	
	// 学生选择老师(无老师时才能选择老师)
	static void selectTeacher(Student student) {
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
			
		Student temp = (Student)session.get(Student.class, student.getId());
		if(temp.getTeacher() == null) {
			temp.setTeacher(student.getTeacher());
		} else {
			System.out.println("该学生已有老师");
		}
		transaction.commit();
		session.close();
	}
		
	// 列出所有学生
	static void listAllStudents() {
		StudentDAO studentDAO = new StudentDAO();
		List<?> students = studentDAO.findAll();
		
		Iterator<?> iterator = students.iterator();
		while(iterator.hasNext()){
			Student student = (Student)iterator.next();
			System.out.println("学生ID：" + student.getId() + "\t名字：" + student.getName());
		}
	}
	
	// 添加老师
	static void newTeacher(Teacher teacher) {
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
			
		session.save(teacher);
		transaction.commit();
		session.close();
	}
	
	// 删除老师(根据主键id)
	static void deleteTeacher(Teacher teacher) {
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
					
		session.delete(teacher);
		transaction.commit();
		session.close();
	}
		
	// 修改老师
	static void updateTeacher(Teacher teacher) {
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
			
		Teacher temp = (Teacher)session.get(Teacher.class, teacher.getId());
		temp.setName(teacher.getName());
		transaction.commit();
		session.close();
	}
		
	// 老师选择学生(可多选)
	static void selectStudent(Teacher teacher, long student_id) {
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
				
		Student temp = (Student)session.get(Student.class, student_id);
		if(temp.getTeacher() == null) {
			temp.setTeacher(teacher);
		} else {
			System.out.println("该学生已有老师");
		}
		transaction.commit();
		session.close();
	}
		
	// 列出所有老师
	static void listAllTeachers() {
		TeacherDAO teacherDAO = new TeacherDAO();
		List<?> teachers = teacherDAO.findAll();
			
		Iterator<?> iterator = teachers.iterator();
		while(iterator.hasNext()){
			Teacher teacher = (Teacher)iterator.next();
			System.out.println("老师ID：" + teacher.getId() + "\t名字：" + teacher.getName());
		}
	}
}

// 菜单类
class Menu1 {
	Scanner input = new Scanner(System.in);
	
	// 显示菜单
	public void showMenu() {
		
		while(true) {
			printSelectionInfo();		
			String selection = input.next();		// 选择
			selectFunction(selection);
		}
	}
	
	// 功能选择
	public void selectFunction(String selection) {
		Student student;
		Teacher teacher;
		
		switch(selection) {
			case "1":	System.out.print("请输入学生名字：");
						String sName = input.next();
						student = new Student();
						student.setName(sName);
						ComprehensionHibernateSpringClient.newStudent(student);
						break;
						
			case "2":   System.out.print("请输入删除学生学号：");
						long sid = input.nextLong();
						student = new Student();
						student.setId(sid);
						ComprehensionHibernateSpringClient.deleteStudent(student);
						break;
						
			case "3": 	System.out.print("请输入修改的学生学号：");
						long sid2 = input.nextLong();
						System.out.print("请输入修改的学生名字：");
						String sName2 = input.next();
						student = new Student();
						student.setId(sid2);
						student.setName(sName2);
						ComprehensionHibernateSpringClient.updateStudent(student);
						break;
						
			case "4":	System.out.print("请输入要选择老师的学生学号：");
						long sid3 = input.nextLong();
						System.out.print("请输入要选择的老师工号：");
						long tid3 = input.nextLong();
						teacher = new Teacher();
						teacher.setId(tid3);
						student = new Student();
						student.setId(sid3);
						student.setTeacher(teacher);
						ComprehensionHibernateSpringClient.selectTeacher(student);
						break;
						
			case "5": 	ComprehensionHibernateSpringClient.listAllStudents();
						break;
						
			case "6":	System.out.print("请输入老师名字：");
						String tName = input.next();
						teacher = new Teacher();
						teacher.setName(tName);
						ComprehensionHibernateSpringClient.newTeacher(teacher);
						break;
						
			case "7":	System.out.print("请输入删除老师工号：");
						long tid2 = input.nextLong();
						teacher = new Teacher();
						teacher.setId(tid2);
						ComprehensionHibernateSpringClient.deleteTeacher(teacher);
						break;
						
			case "8":	System.out.println("请输入修改的老师工号：");
						long tid4 = input.nextLong();
						System.out.println("请输入修改的老师名字：");
						String tName4 = input.next();
						teacher = new Teacher();
						teacher.setId(tid4);
						teacher.setName(tName4);
						ComprehensionHibernateSpringClient.updateTeacher(teacher);
						break;
						
			case "9":	System.out.print("请输入要选择学生的老师工号：");
						long tid5 = input.nextLong();
						System.out.print("请输入要选择的学生学号：");
						long sid5 = input.nextLong();
						teacher = new Teacher();
						teacher.setId(tid5);
						ComprehensionHibernateSpringClient.selectStudent(teacher, sid5);
						break;
						
			case "10":	ComprehensionHibernateSpringClient.listAllTeachers();
						break;
				
			case "e": 	System.out.println("成功退出程序");
						System.exit(0);
						
			default :	System.out.println("输入错误");
		}
	}
		
	// 显示选项信息
	public void printSelectionInfo() {
			
		System.out.println();
		System.out.println("请选择：");
		System.out.println("1. 添加学生");
		System.out.println("2. 删除学生");
		System.out.println("3. 修改学生");
		System.out.println("4. 学生选择老师");
		System.out.println("5. 显示学生表信息");
		System.out.println("6. 添加老师");
		System.out.println("7. 删除老师");
		System.out.println("8. 修改老师");
		System.out.println("9. 老师选择学生");
		System.out.println("10.显示老师表信息");
		System.out.println("e. 退出程序");
	}
}